package logic

import (
	"box/boxrpc/boxrpc"
	"box/boxrpc/internal/svc"
	"box/models"
	"context"

	"github.com/zeromicro/go-zero/core/logx"
)

type ClassAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewClassAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ClassAddLogic {
	return &ClassAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 分类添加
func (l *ClassAddLogic) ClassAdd(in *boxrpc.ClassAddRequest) (*boxrpc.ClassAddResponse, error) {
	// todo: add your logic here and delete this line

	var class models.Class
	models.DB.Debug().Where("className", in.ClassName).Find(&class)
	if class.ClassName == "" {
		class = models.Class{
			ClassName: in.ClassName,
		}
		models.DB.Debug().Create(&class)
	}

	return &boxrpc.ClassAddResponse{
		ClassId: int64(class.ID),
	}, nil
}
